package cn.ztuo.bitrade.service;


import cn.ztuo.bitrade.Trader.CoinTrader;
import cn.ztuo.bitrade.Trader.CoinTraderFactory;
import cn.ztuo.bitrade.entity.ExchangeCoin;
import cn.ztuo.bitrade.entity.ExchangeOrder;
import cn.ztuo.bitrade.entity.ExchangeOrderDirection;
import cn.ztuo.bitrade.runnable.RobotOrderRunnable;
import cn.ztuo.bitrade.util.ExchangeUtils;
import cn.ztuo.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
public class ExchangeService {
    @Autowired
    private CoinTraderFactory traderFactory;
    //机器人下订单
    public static final ExecutorService executor = Executors.newCachedThreadPool();

    public MessageResult robotAddOrder(String symbol) {
        List<ExchangeCoin> coins = ExchangeUtils.findExchangeCoins();
        Map<String, Map<ExchangeOrderDirection, List<ExchangeOrder>>> plateMap = RobotOrderRunnable.plateMap;
        if (StringUtils.isBlank(symbol)) {
            coins.stream().forEach(coin -> {
                CoinTrader coinTrader = traderFactory.getTrader(symbol);
                if (!plateMap.isEmpty() && plateMap.containsKey(coin.getSymbol()) && plateMap.get(coin.getSymbol()) != null) {
                    Map<ExchangeOrderDirection, List<ExchangeOrder>> mapPlate = plateMap.get(coin.getSymbol());
                    List<ExchangeOrder> bidsOld = mapPlate.get(ExchangeOrderDirection.BUY);
                    List<ExchangeOrder> asksOld = mapPlate.get(ExchangeOrderDirection.SELL);
                    coinTrader.cancelOrder(bidsOld, ExchangeOrderDirection.BUY, 2);
                    coinTrader.cancelOrder(asksOld, ExchangeOrderDirection.SELL, 2);
                }
                RobotOrderRunnable robotOrderRunnable = new RobotOrderRunnable(coin, coinTrader, this);
                executor.execute(robotOrderRunnable);
            });
        } else {
            synchronized (symbol) {
                CoinTrader coinTrader = traderFactory.getTrader(symbol);
                ExchangeCoin coin = coins.stream().filter(a -> a.getSymbol().equals(symbol)).findAny().get();
                if (!plateMap.isEmpty() && plateMap.containsKey(coin.getSymbol()) && plateMap.get(coin.getSymbol()) != null) {
                    Map<ExchangeOrderDirection, List<ExchangeOrder>> mapPlate = plateMap.get(coin.getSymbol());
                    List<ExchangeOrder> bidsOld = mapPlate.get(ExchangeOrderDirection.BUY);
                    List<ExchangeOrder> asksOld = mapPlate.get(ExchangeOrderDirection.SELL);
                    coinTrader.cancelOrder(bidsOld, ExchangeOrderDirection.BUY, 2);
                    coinTrader.cancelOrder(asksOld, ExchangeOrderDirection.SELL, 2);
                }
                RobotOrderRunnable robotOrderRunnable = new RobotOrderRunnable(coin, coinTrader, this);
                executor.execute(robotOrderRunnable);
            }
        }
        return MessageResult.success(symbol + "当前机器人-------------------------------");
    }

    public synchronized MessageResult robotAddOrder(ExchangeCoin coin) {
        CoinTrader coinTrader = traderFactory.getTrader(coin.getSymbol());
        RobotOrderRunnable robotOrderRunnable = new RobotOrderRunnable(coin, coinTrader, this);
        executor.execute(robotOrderRunnable);
        return MessageResult.success(coin.getSymbol() + "当前机器人-------------------------------");
    }
}
